package com.base.project.deeplinks

import android.app.Activity
import android.content.Intent
import com.blankj.utilcode.util.LogUtils
import com.facebook.FacebookSdk
import com.facebook.applinks.AppLinkData
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume


/**
 * Created by Orange on 2022/12/4 17:50.
 * facebook 延迟深度链接
 */
class FacebookTask : Task {

    override suspend fun asyncGetUrl(activity: Activity, intent: Intent?) =
        suspendCancellableCoroutine { continuation ->
            // https://developers.facebook.com/docs/app-ads/deep-linking/
            FacebookSdk.setAutoInitEnabled(true)
            FacebookSdk.fullyInitialize()
            if (FacebookSdk.isInitialized()) {
                LogUtils.d("Fb 初始化成功")
                AppLinkData.fetchDeferredAppLinkData(activity) { appLinkData: AppLinkData? ->
                    appLinkData?:continuation.resume(null)
                    appLinkData?.let {
                        continuation.resume(LinkData(uri = appLinkData.targetUri, tag = "facebook"))
                    }
                }
            } else {
                continuation.resume(null)
                LogUtils.d("Fb 没有初始化")
            }
        }
}